home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / lib / savedir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-21  |  3.0 KB  |  127 lines

  1. /* savedir.c -- save the list of files in a directory in a string
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@ai.mit.edu>. */
  19.  
  20. #include <sys/types.h>
  21. #ifdef DIRENT
  22. #include <dirent.h>
  23. #ifdef direct
  24. #undef direct
  25. #endif
  26. #define direct dirent
  27. #define NLENGTH(direct) (strlen((direct)->d_name))
  28. #else
  29. #define NLENGTH(direct) ((direct)->d_namlen)
  30. #ifdef USG
  31. #ifdef SYSNDIR
  32. #include <sys/ndir.h>
  33. #else
  34. #include <ndir.h>
  35. #endif
  36. #else
  37. #include <sys/dir.h>
  38. #endif
  39. #endif
  40.  
  41. #ifdef VOID_CLOSEDIR
  42. /* Fake a return value. */
  43. #define CLOSEDIR(d) (closedir (d), 0)
  44. #else
  45. #define CLOSEDIR(d) closedir (d)
  46. #endif
  47.  
  48. #ifdef STDC_HEADERS
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #else
  52. char *malloc ();
  53. char *realloc ();
  54. int strlen ();
  55. #ifndef NULL
  56. #define NULL 0
  57. #endif
  58. #endif
  59.  
  60. char *stpcpy ();
  61.  
  62. /* Return a freshly allocated string containing the filenames
  63.    in directory DIR, separated by '\0' characters;
  64.    the end is marked by two '\0' characters in a row.
  65.    NAME_SIZE is the number of bytes to initially allocate
  66.    for the string; it will be enlarged as needed.
  67.    Return NULL if DIR cannot be opened or if out of memory. */
  68.  
  69. char *
  70. savedir (dir, name_size)
  71.      char *dir;
  72.      unsigned name_size;
  73. {
  74.   DIR *dirp;
  75.   struct direct *dp;
  76.   char *name_space;
  77.   char *namep;
  78.  
  79.   dirp = opendir (dir);
  80.   if (dirp == NULL)
  81.     return NULL;
  82.  
  83.   name_space = (char *) malloc (name_size);
  84.   if (name_space == NULL)
  85.     {
  86.       closedir (dirp);
  87.       return NULL;
  88.     }
  89.   namep = name_space;
  90.  
  91.   while ((dp = readdir (dirp)) != NULL)
  92.     {
  93.       /* Skip "." and ".." (some NFS filesystems' directories lack them). */
  94.       if (dp->d_name[0] != '.'
  95.       || (dp->d_name[1] != '\0'
  96.           && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
  97.     {
  98.       unsigned size_needed = (namep - name_space) + NLENGTH (dp) + 2;
  99.  
  100.       if (size_needed > name_size)
  101.         {
  102.           char *new_name_space;
  103.  
  104.           while (size_needed > name_size)
  105.         name_size += 1024;
  106.  
  107.           new_name_space = realloc (name_space, name_size);
  108.           if (new_name_space == NULL)
  109.         {
  110.           closedir (dirp);
  111.           return NULL;
  112.         }
  113.           namep += new_name_space - name_space;
  114.           name_space = new_name_space;
  115.         }
  116.       namep = stpcpy (namep, dp->d_name) + 1;
  117.     }
  118.     }
  119.   *namep = '\0';
  120.   if (CLOSEDIR (dirp))
  121.     {
  122.       free (name_space);
  123.       return NULL;
  124.     }
  125.   return name_space;
  126. }
  127.